<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\CompanyMessageFormat;
use App\Http\Requests\CreateCompanyMessageRequest;
use App\Services\CompanyMessageService;
use Illuminate\Http\Request;

class CompanyMessageController extends Controller
{
    protected $companyMessageService;

    public function __construct(CompanyMessageService $companyMessageService)
    {
        $this->companyMessageService = $companyMessageService;
    }

    public function getLists(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_data')) {
            $search['end_date'] = $request->input('end_date');
        }
        $res = $this->companyMessageService->getList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $res = $this->companyMessageService->detail($id);
        return renderSuccessJson($res);
    }

    public function create(CreateCompanyMessageRequest $createCompanyMessageRequest)
    {
        $format = new CompanyMessageFormat($createCompanyMessageRequest->all());
        $this->companyMessageService->create($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $format = new CompanyMessageFormat($request->all());
        $this->companyMessageService->update($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $res = $this->companyMessageService->delete($id);
        return renderSuccessJson($res);
    }
}
